﻿Imports DevComponents.DotNetBar

Public Class ucCapnhatcongty
    Private eActive As Integer
    Private _dataRow As DataGridViewRow
    Private _comBUS As New CompanyBUS()
    Event Closed(sender As Object)

    Private iscancel As Boolean
    Public Property Canceled() As Boolean
        Get
            Return iscancel
        End Get
        Set(ByVal value As Boolean)
            iscancel = value
        End Set
    End Property



    Private Function ValidateData() As Boolean
        Dim validate As Boolean = True
        If txtTencongty.Text = "" Then
            validate = False
            ErrorProvider1.SetError(txtTencongty, "Vui lòng nhập tên công ty")
        Else
            ErrorProvider1.SetError(txtTencongty, "")
        End If
        If txtDiachi.Text = "" Then
            validate = False
            ErrorProvider1.SetError(txtDiachi, "Vui lòng nhập địa chỉ công ty")
        Else
            ErrorProvider1.SetError(txtDiachi, "")
        End If
        If txtSDT1.Text = "" Then
            validate = False
            ErrorProvider1.SetError(txtSDT1, "Vui lòng nhập số điện thoại")
        Else
            ErrorProvider1.SetError(txtSDT1, "")
        End If

        Return validate
    End Function


    Private Sub ButtonItemThoat_Click(sender As Object, e As EventArgs) Handles ButtonItemThoat.Click
        SuperValidator1.Enabled = False

        Dim parent = DirectCast(My.Application.OpenForms.Item(0), MetroAppForm)
        parent.CloseModalPanel(Me, DevComponents.DotNetBar.Controls.eSlideSide.Right)
        Canceled = True
        RaiseEvent Closed(Me)
    End Sub

    Private Sub ButtonItemSave_Click(sender As Object, e As EventArgs) Handles ButtonItemSave.Click
        If ValidateData() Then
            'MessageBoxEx.Show("true", "test", MessageBoxButtons.OK)
            If eActive = 0 Then
                _comBUS.Company_Insert(txtTencongty.Text, _
                                       txtDiachi.Text, _
                                       txtSDT1.Text, _
                                       txtSDT2.Text, _
                                       txtDTDD.Text, _
                                       txtFax.Text, _
                                       txtEmail.Text, _
                                       txtWebsite.Text, _
                                       txtGhichu.Text, _
                                       True, _
                                       "createby", _
                                       Date.Now, _
                                       "modified by", _
                                       Date.Now, _
                                       txtMasothue.Text)

                BalloonTip1.SetBalloonCaption(ItemPanel1, "Thông báo")
                BalloonTip1.SetBalloonText(ItemPanel1, "Thêm công ty " + txtTencongty.Text + " thành công")
                BalloonTip1.ShowBalloon(ItemPanel1)
            ElseIf eActive = 1 Then
                _comBUS.Company_Update(Integer.Parse(_dataRow.Cells(1).Value.ToString()), _
                                       txtTencongty.Text, _
                                       txtDiachi.Text, _
                                       txtSDT1.Text, _
                                       txtSDT2.Text, _
                                       txtDTDD.Text, _
                                       txtFax.Text, _
                                       txtEmail.Text, _
                                       txtWebsite.Text, _
                                       txtGhichu.Text, _
                                       Boolean.Parse(_dataRow.Cells(11).Value), _
                                       _dataRow.Cells(12).Value.ToString(), _
                                       Date.Parse(_dataRow.Cells(13).Value), _
                                       "modifiedby", _
                                       Date.Now, _
                                       txtMasothue.Text)
                BalloonTip1.SetBalloonCaption(ItemPanel1, "Thông báo")
                BalloonTip1.SetBalloonText(ItemPanel1, "Cập nhật công ty " + txtTencongty.Text + " thành công")
                BalloonTip1.ShowBalloon(ItemPanel1)
            End If
            Canceled = True
            RaiseEvent Closed(Me)
        Else
            'MessageBoxEx.Show("false", "test", MessageBoxButtons.OK)
        End If
    End Sub

    Public Sub New(ByVal eAct As Integer, _
                   ByVal _row As DataGridViewRow)
        MyBase.New()
        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        eActive = eAct

        If eAct = 0 Then
            lbTieude.Text = "Thêm nhà sản xuất mới"
        ElseIf eAct = 1 Then
            lbTieude.Text = "Cập nhật nhà sản xuất"
            _dataRow = _row
            txtTencongty.Text = _dataRow.Cells(0).Value.ToString()
            txtMasothue.Text = _dataRow.Cells(3).Value.ToString()
            txtDiachi.Text = _dataRow.Cells(2).Value.ToString()
            txtSDT1.Text = _dataRow.Cells(4).Value.ToString()
            txtSDT2.Text = _dataRow.Cells(5).Value.ToString()
            txtDTDD.Text = _dataRow.Cells(6).Value.ToString()
            txtFax.Text = _dataRow.Cells(7).Value.ToString()
            txtEmail.Text = _dataRow.Cells(9).Value.ToString()
            txtWebsite.Text = _dataRow.Cells(10).Value.ToString()
            txtGhichu.Text = _dataRow.Cells(8).Value.ToString()
        End If

    End Sub
End Class
